/**
 * 更改easyui加载panel时的提示文字
 *
 * @author cy
 *
 * @requires jQuery,EasyUI
 */
$.extend($.fn.panel.defaults, {
    loadingMessage : '加载中....'
});

/**
 * 更改easyui加载grid时的提示文字
 *
 * @author cy
 *
 * @requires jQuery,EasyUI
 */
$.extend($.fn.datagrid.defaults, {
    loadMsg : '数据加载中....'
});

$.extend($.fn.validatebox.defaults.rules, {
    equals : {
        validator : function(value, param) {
            return value == $(param[0]).val();
        },
        message : '密码不一致!'
    }
});

/*
 * 节点树的扩展
 */

$.fn.tree.defaults.loadFilter = function(data, parent) {
    var opt = $(this).data().tree.options;
    var idFiled, textFiled, parentField;
    if (opt.parentField) {
        idFiled = opt.idFiled || 'id';
        textFiled = opt.textFiled || 'text';
        parentField = opt.parentField;
        var i, l, treeData = [], tmpMap = [];
        for (i = 0, l = data.length; i < l; i++) {
            tmpMap[data[i][idFiled]] = data[i];
        }
        for (i = 0, l = data.length; i < l; i++) {
            if (tmpMap[data[i][parentField]] && data[i][idFiled] != data[i][parentField]) {
                if (!tmpMap[data[i][parentField]]['children'])
                    tmpMap[data[i][parentField]]['children'] = [];
                data[i]['text'] = data[i][textFiled];
                tmpMap[data[i][parentField]]['children'].push(data[i]);
            } else {
                data[i]['text'] = data[i][textFiled];
                treeData.push(data[i]);
            }
        }
        return treeData;
    }
    return data;
};


/**
 * @author 孙宇
 *
 * @requires jQuery,EasyUI
 *
 * 防止panel/window/dialog组件超出浏览器边界
 * @param left
 * @param top
 */
var easyuiPanelOnMove = function(left, top) {
    var l = left;
    var t = top;
    if (l < 1) {
        l = 1;
    }
    if (t < 1) {
        t = 1;
    }
    var width = parseInt($(this).parent().css('width')) + 14;
    var height = parseInt($(this).parent().css('height')) + 14;
    var right = l + width;
    var buttom = t + height;
    var browserWidth = $(window).width();
    var browserHeight = $(window).height();
    if (right > browserWidth) {
        l = browserWidth - width;
    }
    if (buttom > browserHeight) {
        t = browserHeight - height;
    }
    $(this).parent().css({/* 修正面板位置 */
        left : l,
        top : t
    });
};
$.fn.dialog.defaults.onMove = easyuiPanelOnMove;
$.fn.window.defaults.onMove = easyuiPanelOnMove;
$.fn.panel.defaults.onMove = easyuiPanelOnMove;

/*
 jQuery EasyUI
 * panel关闭时回收内存
 * */
$.fn.panel.defaults.onBeforeDestroy = function () {
    var frame = $('iframe', this);
    try {
        if (frame.length > 0) {
            for (var i = 0; i < frame.length; i++) {
                frame[i].contentWindow.document.write('');
                frame[i].contentWindow.close();
            }
            frame.remove();
            if ($.browser.msie) {
                CollectGarbage();
            }
        }
    } catch (e) {
    }
};

//在layout的panle全局配置中,增加一个onCollapse处理title

$.extend($.fn.layout.paneldefaults, {

    onCollapse : function () {

        //获取layout容器

        var layout = $(this).parents(".layout");

        //获取当前region的配置属性

        var opts = $(this).panel("options");

        //获取key

        var expandKey = "expand" + opts.region.substring(0, 1).toUpperCase() + opts.region.substring(1);

        //从layout的缓存对象中取得对应的收缩对象

        var expandPanel = layout.data("layout").panels[expandKey];

        //针对横向和竖向的不同处理方式

        if (opts.region == "west" || opts.region == "east") {

            //竖向的文字打竖,其实就是切割文字加br

            var split = [];

            for (var i = 0; i < opts.title.length; i++) {

                split.push(opts.title.substring(i, i + 1));

            }

            expandPanel.panel("body").addClass("panel-title").css("text-align", "center").html(split.join("<br>"));

        } else {

            expandPanel.panel("setTitle", opts.title);

        }

    }

});

/**
 * 创建一个模式化的dialog
 *
 * @author 陈咏
 *
 * @requires jQuery,EasyUI
 *
 */
modalDialog = function(options) {
    var opts = $.extend({
        title : '&nbsp;',
        width : 640,
        height : 480,
        modal : true,
        onClose : function() {
            $(this).dialog('destroy');
        }
    }, options);
    opts.modal = true;// 强制此dialog为模式化，无视传递过来的modal参数
    if (options.url) {
        opts.content = '<iframe id="" src="' + options.url + '" allowTransparency="true" scrolling="auto" width="100%" height="98%" frameBorder="0" name=""></iframe>';
    }
    return $('<div/>').dialog(opts);
};

